var express = require('express')
  , path = require('path')
  , requireNoCache = require('../../tools/require-nocache');

module.exports = function(server){

  //server.use('/tpm/account/:userId/infrastructure',requireNoCache(path.join(process.cwd(),'serverMocks/infrastructure/index.js')));

  server.use(function(req,res,next){
    if(/\.json$/.test(require('url').parse(req.url,true).pathname)){
      if(req.query.spanTime){
        var spanTime = parseInt(req.query.spanTime)||30*60*1000
          , end = req.query.end = parseInt(req.query.end)||Date.now()
          , begin = req.query.begin = (new Date(end - spanTime)).getTime()
          , interval = parseInt(req.query.interval)||spanTime
          , size = req.query.size = spanTime/interval
      }
      var jsonHandler = express.Router();
      jsonHandler.use(requireNoCache(path.join(__dirname, '..', '..', 'mocks', 'index')));
      setTimeout(function(){
        jsonHandler(req,res,next);
      },0);
    }else{
      next()
    }
  })
};